package service.impl;

/**
 * @author曹桢瑞

/*
import org.dmg.pmml.FieldName;
import org.dmg.pmml.PMML;
import org.jpmml.evaluator.*;
*/

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import po.Book;
import service.BookService;
import service.RecommendService;
import util.CommonUtil;

public class ALSServiceImpl implements RecommendService {

	/* (non-Javadoc)
	 * @see service.RecommendService#getBookList(java.lang.String)
	 */
	@Override
	public List<Book> getBookList(String userId,int num) {
		Evaluator evaluator=loadPmml();
		List<String> bookIdList=predict(evaluator,userId);
		ArrayList<Book> list=new ArrayList<Book>();
		BookService bookService=new BookServiceImpl();
		if(bookIdList.size()<=num) {
			num=bookIdList.size();
			for(int i=0;i<num;i++) {
				String bookId=bookIdList.get(i);
				Book book=bookService.getBookInfoById(bookId).get(0);
				list.add(book);
			}
		}
		else {
			HashSet<Integer> set=new HashSet<Integer>();
			CommonUtil.randomSet(0,bookIdList.size(),num,set);
			Iterator<Integer> it = set.iterator();
			while(it.hasNext()) {
				String bookId=bookIdList.get(it.next());
				Book temp=bookService.getBookInfoById(bookId).get(0);
				list.add(temp);
			}
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see service.RecommendService#getUserList(java.lang.String)
	 */
	@Override
	public List<Book> getUserList(String userId) {
		Evaluator evaluator=loadPmml();
		List<String> bookIdList=predict(evaluator,userId);
		ArrayList<Book> list=new ArrayList<Book>();
		for(int i=0;i<bookIdList.size();i++) {
			String bookId=bookIdList.get(i);
			BookService bookService=new BookServiceImpl();
			Book book=bookService.getBookInfoById(bookId).get(0);
			list.add(book);
		}
		return list;
	}
	
	private static Evaluator loadPmml() {
		PMML pmml = new PMML();
		String path="/Users/xingoo/Desktop/test2.xml";
		try(InputStream inputStream = new FileInputStream(path)){
            pmml = org.jpmml.model.PMMLUtil.unmarshal(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
        return modelEvaluatorFactory.newModelEvaluator(pmml);
	}
	private static Object predict(Evaluator evaluator,String userId) {
        Map<String, Integer> data = new HashMap<String, Integer>();
        /*
        data.put("field_0", a);
        data.put("field_1", b);
        data.put("field_2", c);
        data.put("field_3", d);
        List<InputField> inputFields = evaluator.getInputFields();
        //��ģ�͵�ԭʼ�������ӻ����л�ȡ���ݣ���Ϊģ������
        Map<FieldName, FieldValue> arguments = new LinkedHashMap<FieldName, FieldValue>();
        for (InputField inputField : inputFields) {
            FieldName inputFieldName = inputField.getName();
            Object rawValue = data.get(inputFieldName.getValue());
            FieldValue inputFieldValue = inputField.prepare(rawValue);
            arguments.put(inputFieldName, inputFieldValue);
        }
 
        Map<FieldName, ?> results = evaluator.evaluate(arguments);
 
        List<TargetField> targetFields = evaluator.getTargetFields();
        TargetField targetField = targetFields.get(0);
        FieldName targetFieldName = targetField.getName();
        ProbabilityDistribution target = (ProbabilityDistribution) results.get(targetFieldName);
        System.out.println(a + " " + b + " " + c + " " + d + ":" + target);
        return target;
        */
        return null;
    }
}
